Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald-tests\blemacaddress-tests.cpp
Line
Count
Source
1
//  Copyright 2020-2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#include <memory>
6
#include <vector>
7
#include <iostream>
8
9
#include "catch.hpp"
10
11
#include "herald/herald.h"
12
13
/**
14
 * Implementation note: BLE Mac should ALWAYS be valid 6 bytes, even if data wrong
15
 */
16
17
1
TEST_CASE("ble-macaddress-basic", "[ble][macaddress][basic]") {
18
1
  SECTION("ble-macaddress-basic") {
19
1
    std::uint8_t data[] {0,1,2,3,4,5}; // mac address data
20
1
    herald::ble::BLEMacAddress mac(data);
21
1
    herald::datatype::Data original = (herald::datatype::Data)mac; // explicit conversion operator test
22
1
    REQUIRE(6 == original.size());
23
1
    REQUIRE(std::byte(0) == original.at(0));
24
1
    REQUIRE(std::byte(1) == original.at(1));
25
1
    REQUIRE(std::byte(2) == original.at(2));
26
1
    REQUIRE(std::byte(3) == original.at(3));
27
1
    REQUIRE(std::byte(4) == original.at(4));
28
1
    REQUIRE(std::byte(5) == original.at(5));
29
1
  }
30
1
}
31
32
1
TEST_CASE("ble-macaddress-ctor-copy", "[ble][macaddress][ctor][copy") {
33
1
  SECTION("ble-macaddress-ctor-copy") {
34
1
    std::uint8_t data[] {0,1,2,3,4,5}; // mac address data
35
1
    herald::ble::BLEMacAddress mac(data);
36
1
    herald::ble::BLEMacAddress maccopy(mac);
37
1
    herald::datatype::Data original = (herald::datatype::Data)maccopy; // explicit conversion operator test
38
1
    REQUIRE(6 == original.size());
39
1
    REQUIRE(std::byte(0) == original.at(0));
40
1
    REQUIRE(std::byte(1) == original.at(1));
41
1
    REQUIRE(std::byte(2) == original.at(2));
42
1
    REQUIRE(std::byte(3) == original.at(3));
43
1
    REQUIRE(std::byte(4) == original.at(4));
44
1
    REQUIRE(std::byte(5) == original.at(5));
45
1
    REQUIRE(mac == maccopy);
46
1
    herald::datatype::Data originalOrig = (herald::datatype::Data)mac;
47
1
    REQUIRE(original == originalOrig); 
48
1
  }
49
1
}
50
51
1
TEST_CASE("ble-macaddress-empty", "[ble][macaddress][empty]") {
52
1
  SECTION("ble-macaddress-empty") {
53
1
    herald::ble::BLEMacAddress mac;
54
1
    herald::datatype::Data original = (herald::datatype::Data)mac; // explicit conversion operator test
55
1
    REQUIRE(6 == original.size());
56
1
    REQUIRE(std::byte(0) == original.at(0));
57
1
    REQUIRE(std::byte(0) == original.at(1));
58
1
    REQUIRE(std::byte(0) == original.at(2));
59
1
    REQUIRE(std::byte(0) == original.at(3));
60
1
    REQUIRE(std::byte(0) == original.at(4));
61
1
    REQUIRE(std::byte(0) == original.at(5));
62
1
  }
63
1
}
64
65
1
TEST_CASE("ble-macaddress-emptydata", "[ble][macaddress][emptydata]") {
66
1
  SECTION("ble-macaddress-emptydata") {
67
1
    herald::ble::Data d;
68
1
    herald::ble::BLEMacAddress mac(d);
69
1
    herald::datatype::Data original = (herald::datatype::Data)mac; // explicit conversion operator test
70
1
    REQUIRE(6 == original.size());
71
1
    REQUIRE(std::byte(0) == original.at(0));
72
1
    REQUIRE(std::byte(0) == original.at(1));
73
1
    REQUIRE(std::byte(0) == original.at(2));
74
1
    REQUIRE(std::byte(0) == original.at(3));
75
1
    REQUIRE(std::byte(0) == original.at(4));
76
1
    REQUIRE(std::byte(0) == original.at(5));
77
1
  }
78
1
}
79
80
1
TEST_CASE("ble-macaddress-smalldata", "[ble][macaddress][smalldata]") {
81
1
  SECTION("ble-macaddress-smalldata") {
82
1
    std::uint8_t data[] {0,1,2,3}; // mac address data - too small! Use what data we can
83
1
    herald::ble::Data d(data,4);
84
1
    herald::ble::BLEMacAddress mac(d);
85
1
    herald::datatype::Data original = (herald::datatype::Data)mac; // explicit conversion operator test
86
1
    REQUIRE(6 == original.size());
87
1
    REQUIRE(std::byte(0) == original.at(0));
88
1
    REQUIRE(std::byte(1) == original.at(1));
89
1
    REQUIRE(std::byte(2) == original.at(2));
90
1
    REQUIRE(std::byte(3) == original.at(3));
91
1
    REQUIRE(std::byte(0) == original.at(4)); // last two empty
92
1
    REQUIRE(std::byte(0) == original.at(5)); // last two empty
93
1
  }
94
1
}
95
96
1
TEST_CASE("ble-macaddress-largedata", "[ble][macaddress][largedata]") {
97
1
  SECTION("ble-macaddress-largedata") {
98
1
    std::uint8_t data[] {0,1,2,3,4,5,6,7,8,9,10,11}; // mac address data - too large!
99
1
    herald::ble::Data d(data,12);
100
1
    herald::ble::BLEMacAddress mac(d);
101
1
    herald::datatype::Data original = (herald::datatype::Data)mac; // explicit conversion operator test
102
1
    REQUIRE(6 == original.size());
103
1
    REQUIRE(std::byte(0) == original.at(0));
104
1
    REQUIRE(std::byte(1) == original.at(1));
105
1
    REQUIRE(std::byte(2) == original.at(2));
106
1
    REQUIRE(std::byte(3) == original.at(3));
107
1
    REQUIRE(std::byte(4) == original.at(4));
108
1
    REQUIRE(std::byte(5) == original.at(5));
109
1
  }
110
1
}
111
112
1
TEST_CASE("ble-macaddress-tostring", "[ble][macaddress][tostring]") {
113
1
  SECTION("ble-macaddress-tostring") {
114
1
    std::uint8_t data[] {0,1,2,3,4,5}; // mac address data
115
1
    herald::ble::Data d(data,6);
116
1
    herald::ble::BLEMacAddress mac(d);
117
1
    std::string description = (std::string)mac; // conversion operator
118
1
    INFO("BLEMacAddress: String description: " << description);
119
1
    REQUIRE(17 == description.size()); // 2 chars per 6 data elements, 5 : char separators
120
1
    REQUIRE("05:04:03:02:01:00" == description); // little endian conversion
121
1
  }
122
1
}